package com.lagou.web.servlet;

import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.Impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;
import com.lagou.utils.UUIDUtils;
import com.mysql.cj.Constants;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SaveCourse")
public class SaveCourseInfo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            Course course = new Course();
            HashMap<String,Object> map = new HashMap<>();
            DiskFileItemFactory factory = new DiskFileItemFactory();

            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> list = upload.parseRequest(req);

            if (list!=null){

            for (FileItem item : list) {
                boolean formField = item.isFormField();
                if (formField) {
                    String fieldName = item.getFieldName();
                    String value = item.getString("utf-8");
                    System.out.println(fieldName + "=" + value);

                    map.put(fieldName, value);

                } else {
                    String name = item.getName();
                    String newFileName = UUIDUtils.getUUID() + "_" + name;

                    InputStream in = item.getInputStream();

                    String realPath = this.getServletContext().getRealPath("/");
                    String path = realPath.substring(0, realPath.indexOf("unit_war"));

                    FileOutputStream out = new FileOutputStream(path + "/public/" + newFileName);

                    IOUtils.copy(in, out);

                    out.close();
                    in.close();

                    map.put("course_img_url","http://localhost:8080" +"/public/" + newFileName);

                    }
                }
            }

            BeanUtils.populate(course,map);

            String dateFormart = DateUtils.getDateFormart();
            CourseService cs = new CourseServiceImpl();


            if (map.get("id")!= null){
                course.setUpdate_time(dateFormart);
                String s = cs.SaveCourseSales(course);
                //响应结果
                resp.getWriter().print(s);
            }else {
                course.setCreate_time(dateFormart);//创建时间
                course.setUpdate_time(dateFormart);//修改时间
               // course.setStatus(1); //上架
                String result = cs.SaveCourseSales(course);
                //响应结果
                resp.getWriter().print(result);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
